package api

import (
	"fmt"
	"strconv"
	"time"
)

var (
	datetime string
)

func init() {
	fmt.Println("api模块初始化成功……")
	now := time.Now()
	var year string = strconv.Itoa(now.Year())
	var month string = strconv.Itoa(int(now.Month()))
	var day string = strconv.Itoa(now.Day())
	var hour string = strconv.Itoa(now.Hour())
	var minute string = strconv.Itoa(now.Minute())
	var second string = strconv.Itoa(now.Second())

	datetime = fmt.Sprintf("%s-%s-%s %s:%s:%s", year, month, day, hour, minute, second)
	fmt.Printf("现在的时间是：%v\n", datetime)

	// 让程序休眠1毫秒
	time.Sleep(time.Millisecond)

	now = time.Now()
	second = strconv.Itoa(now.Second())
	datetime = now.Format("2006/01/02 15:04:05")
	fmt.Printf("现在的时间是:%v\n", datetime)
}

func Welcome() {
	fmt.Println("陈文小超venki.chen欢迎您来到今日golang语言学习……")
}

func Access() {
	// map-relation
	// mapStudy()
	meetStudy()
}

// 面向对象
func meetStudy() {
	var a bool
	fmt.Printf("布尔数据类型默认值是：%v\n", a)
}

// map学习
func mapStudy() {
	// 声明map
	var a map[string]string

	// 初始化map
	a = make(map[string]string, 10)
	a["chen"] = "venki.chen"
	a["wen"] = "venki.wen"
	a["xiao"] = "venki.xiao"
	a["chao"] = "venki.chao"
	fmt.Println("map声明方式1：")
	fmt.Println(a)

	// map 声明方式2
	cities := make(map[string]string)
	cities["01"] = "北京"
	cities["02"] = "上海"
	cities["03"] = "深圳"
	fmt.Println("map声明方式2：")
	fmt.Println(cities)

	// map 声明方式3
	heroes := map[string]string{
		"hero1": "陈",
		"hero2": "文",
		"hero3": "小",
		"hero4": "超",
	}
	fmt.Println("map声明方式3：")
	fmt.Println(heroes)

	// 案例；学生姓名和性别
	students := make(map[string]map[string]string)

	students["stu01"] = make(map[string]string, 3)
	students["stu01"]["name"] = "陈文小超1"
	students["stu01"]["sex"] = "男"

	students["stu02"] = make(map[string]string, 3)
	students["stu02"]["name"] = "陈文小超2"
	students["stu02"]["sex"] = "男"

	students["stu03"] = make(map[string]string, 3)
	students["stu03"]["name"] = "陈文小超3"
	students["stu03"]["sex"] = "男"

	fmt.Println(students)

	fmt.Println("-----------------")

	// map操作
	// delete(cities,"") // 删除
	val, ok := cities["02"]
	if ok {
		fmt.Println(ok)
		fmt.Println(val)
	} else {
		fmt.Println("不存在")
	}

	// map 遍历
	for index, value := range cities {
		fmt.Println(index, value)
	}

	for index, value := range students {
		fmt.Println("{")
		fmt.Println(index, value)
		for key, val := range value {
			fmt.Println(key, val)
		}
		fmt.Println("}")
	}

	fmt.Printf("cities的长度是：%v\n", len(cities))
	fmt.Printf("students的长度是：%v\n", len(students))

	fmt.Println("-------------")

	// 案例
	var monsters []map[string]string
	monsters = make([]map[string]string, 2)
	if monsters[0] == nil {
		monsters[0] = make(map[string]string)
		monsters[0]["name"] = "狐狸精1"
		monsters[0]["age"] = "501"
	}

	if monsters[1] == nil {
		monsters[1] = make(map[string]string)
		monsters[1]["name"] = "狐狸精2"
		monsters[1]["age"] = "502"
	}

	newMonster := map[string]string{
		"name": "陈文小超",
		"age":  "1000",
	}

	monsters = append(monsters, newMonster)
	fmt.Println(monsters)

	fmt.Println("-----------------")
	// map排序
	map1 := make(map[int]int, 10)
	map1[10] = 1
	map1[11] = 2
	map1[22] = 3
	map1[19] = 4
	map1[3] = 5
	map1[15] = 6
	fmt.Println(map1)
	fmt.Println()
	map1[8] = 8
	fmt.Println(map1)

	// map 综合案例
	users := make(map[string]map[string]string, 10)
	res1 := modifyUser(users, "venki")
	fmt.Println(res1)
	res2 := modifyUser(users, "venki")
	fmt.Println(res2)

}

func modifyUser(user map[string]map[string]string, name string) map[string]map[string]string {
	fmt.Println(user[name])
	fmt.Println(user[name] != nil)
	if user[name] != nil {
		user[name]["pws"] = "888888"
	} else {
		user[name] = make(map[string]string)
		user[name]["pwd"] = "666666"
		user[name]["nickname"] = "昵称" + name
	}

	return user
}
